// pages/detail/detail.js
import {addComment,cancelCommentLike,commentLike,cancelLike,likeSuccessfully, articleDetails,getComments,followUsers,accessUser,collectArticles,cancelCollection } from "../../api/news.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:null,
    detailsList:[],
    obj:{},
    commentList:[],
    flag:true,
    show:true,
    WriteComments:false,
    textAreaValue:null
  },
  //发布评论
  async postComments() {
    if(this.data.textAreaValue){
      await addComment({target:this.data.detailsList.art_id,content:this.data.textAreaValue})
      this.setData({
        textAreaValue:null,
        WriteComments:false
      })
      const res = await getComments({type:'a',source:this.data.userId,limit:10})
    res.data.results.forEach(item=>{
                  let newtime=new Date().getTime()
                  let old=new Date(item.pubdate).getTime()
                  const a =newtime-old
                  if(a<=1000*60){
                      item.pubdate = Math.floor(a/1000)+"秒前"
                  }else if(a>1000*60&&a<=1000*60*60){
                      item.pubdate = Math.floor(a/(1000*60))+"分钟前"
                  }else if(a>1000*60*60&&a<=1000*60*60*24){
                      item.pubdate = Math.floor(a/(1000*60*60))+"小时前"
                  }else{
                      item.pubdate = Math.floor(a/(1000*60*60*24))+"天前"
                  }
              })
    // console.log(res);
    this.setData({
      commentList:res.data.results,
      obj:res.data
    })
    this.getArticleDetails()
    }
  },
  // textarea input事件
  textAreaInput(e){
    // console.log(e);
    this.setData({
      textAreaValue:e.detail.value
    })
  },
  //写评论
  WriteComments() {
    this.setData({
      WriteComments:true
    })
  },
  // 对评论或评论回复点赞
  async onCommentLike(e) {
    const likeCount = e.currentTarget.dataset.like_count
    const comId = e.currentTarget.dataset.com_id
    const res = await commentLike({target:comId})
    await this.getComments1()
    const a = this.data.commentList.find(item=>item.com_id===comId)
    if(a.like_count===likeCount) {
      await cancelCommentLike(comId)
      await this.getComments1()
    }
  },
  //点赞
  async onFabulous() {
    const res = await likeSuccessfully({target:this.data.detailsList.art_id})
    console.log(res);
    this.getArticleDetails()
  },
  //取消点赞
  async onCancelLike(){
    const res = await cancelLike(this.data.detailsList.art_id)
    console.log(res);
    this.getArticleDetails()
  },
  //取消收藏
  async onCancelCollection() {
    const res = await cancelCollection(this.data.userId)
    this.getArticleDetails()
  },
  //收藏用户
  async onCollection() {
    const res = await collectArticles({target:this.data.userId})
    this.getArticleDetails()
  },
  //关注用户
  async followUsers() {
    const res = await followUsers({target:this.data.detailsList.aut_id})
    // console.log(res);
    this.getArticleDetails()
  },
  //取关用户
  async accessUser() {
    const res = await accessUser(this.data.detailsList.aut_id)
    this.getArticleDetails()
  },
  async getArticleDetails() {
    const res = await articleDetails(this.data.userId)
    res.data.content = res.data.content.replace(/\<pre/g,'<pre style="background-color: #f6f8fa;overflow: auto;margin:15px;"')
        res.data.content = res.data.content.replace(/\<ol/g,'<ol style="list-style: none;margin-top:20px"')
        res.data.content = res.data.content.replace(/\<ul/g,'<ul style="list-style: none;margin-top:20px"')
        res.data.content = res.data.content.replace(/\<h1/g,'<h1 style="font-size: 16px;font-weight: 600;height:30px;line-height:30px;"')
        res.data.content = res.data.content.replace(/\<h2/g,'<h2 style="border-bottom:1px solid #dfe2e5;font-size: 12px;font-weight: 600;height:30px;line-height:30px;"')
        res.data.content = res.data.content.replace(/\<h3/g,'<h3 style="font-size: 16px;font-weight: 600;height:30px;line-height:30px;"')
        res.data.content = res.data.content.replace(/\<h4/g,'<h4 style="font-size: 16px;font-weight: 600;height:30px;line-height:30px;"')
        res.data.content = res.data.content.replace(/\<img/g,'< img style="width:750rpx;height:400rpx"')
        res.data.content = res.data.content.replace(/\<blockquote/g,'<blockquote style="border-left:1px solid #dfe2e5;padding: 0 12px; margin-top:10px"')
    this.setData({
      detailsList:res.data
    })
    // console.log(res);
  },
  //获取评论
  async getComments1(){
    const res = await getComments({type:'a',source:this.data.userId,limit:10})
    res.data.results.forEach(item=>{
                  let newtime=new Date().getTime()
                  let old=new Date(item.pubdate).getTime()
                  const a =newtime-old
                  if(a<=1000*60){
                      item.pubdate = Math.floor(a/1000)+"秒前"
                  }else if(a>1000*60&&a<=1000*60*60){
                      item.pubdate = Math.floor(a/(1000*60))+"分钟前"
                  }else if(a>1000*60*60&&a<=1000*60*60*24){
                      item.pubdate = Math.floor(a/(1000*60*60))+"小时前"
                  }else{
                      item.pubdate = Math.floor(a/(1000*60*60*24))+"天前"
                  }
              })
    // console.log(res);
    this.setData({
      commentList:res.data.results,
      obj:res.data
    })
  },
  async getComments(){
    if(!this.data.flag) return
    if(this.data.commentList.length < 10) {
      const res = await getComments({type:'a',source:this.data.userId,limit:10})
    res.data.results.forEach(item=>{
                  let newtime=new Date().getTime()
                  let old=new Date(item.pubdate).getTime()
                  const a =newtime-old
                  if(a<=1000*60){
                      item.pubdate = Math.floor(a/1000)+"秒前"
                  }else if(a>1000*60&&a<=1000*60*60){
                      item.pubdate = Math.floor(a/(1000*60))+"分钟前"
                  }else if(a>1000*60*60&&a<=1000*60*60*24){
                      item.pubdate = Math.floor(a/(1000*60*60))+"小时前"
                  }else{
                      item.pubdate = Math.floor(a/(1000*60*60*24))+"天前"
                  }
              })
    // console.log(res);
    this.setData({
      commentList:res.data.results,
      obj:res.data
    })
    }
    if(this.data.obj.total_count > 10) {
      const res1 = await getComments({offset:this.data.obj.last_id,type:'a',source:this.data.userId,limit:10})
      res1.data.results.forEach(item=>{
                    let newtime=new Date().getTime()
                    let old=new Date(item.pubdate).getTime()
                    const a =newtime-old
                    if(a<=1000*60){
                        item.pubdate = Math.floor(a/1000)+"秒前"
                    }else if(a>1000*60&&a<=1000*60*60){
                        item.pubdate = Math.floor(a/(1000*60))+"分钟前"
                    }else if(a>1000*60*60&&a<=1000*60*60*24){
                        item.pubdate = Math.floor(a/(1000*60*60))+"小时前"
                    }else{
                        item.pubdate = Math.floor(a/(1000*60*60*24))+"天前"
                    }
                })
      this.setData({
        commentList:[...this.data.commentList,...res1.data.results],
        obj:res1.data
      })
      if(this.data.obj.total_count === this.data.commentList.length) {
        this.setData({
          flag:false,
          show:true
        })
      }
    } else{
      this.setData({
        flag:false,
        show:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //  console.log(options);
     this.setData({
      userId:options.id
     })
     this.getArticleDetails()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    this.getComments()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  }
})